mini.parse();

function GoodsTypeHandler() {
};

GoodsTypeHandler.instance = new GoodsTypeHandler();

GoodsTypeHandler.isQueryBtnExpended = false;

//获得全局的商品类型数据表格对象
GoodsTypeHandler.goodsTypeGrid = mini.get("goodsTypeGrid");

//加载表格数据
GoodsTypeHandler.goodsTypeGrid.load();

//类型状态数据
GoodsTypeHandler.typeStateData = [
	{text: '全部', id: -1},
	{text: '无效', id: 0},
	{text: '有效', id: 1}
];

mini.get("bgtState").setData(GoodsTypeHandler.typeStateData);

/**
 * 根据用户权限展示功能按钮
 */
GoodsTypeHandler.loadToolbar = function() {
	var array = new Array("goods_type_add", "goods_type_edit", "goods_type_del","goods_type_query","goods_type_refresh");
	var userArr = MiniuiTools.isHasPurview(array);
	for (var i = 0; i < userArr.length; i++) {
		mini.get(array[i]).setVisible(userArr[i]);
	}
};
GoodsTypeHandler.loadToolbar();

/**
 * 弹出添加商品类型窗口
 */
GoodsTypeHandler.addGoodsType = function() {
	miniWin.afreshNewWin();
	miniWin.setParams(341, 170, "添加商品类型");
	miniWin.setOption("bodyStyle","padding:0px;");

	miniWin.load("goodsTypeForm.jsp",function(){
		var iframe = miniWin.getIFrameEl();
        var data = {action: "new"};
        iframe.contentWindow.GoodsTypeFormHandler.passData(data);
	},function(action){
		GoodsTypeHandler.goodsTypeGrid.reload();
	});
	
	miniWin.show();
};

/**
 * 弹出编辑商品类型窗口
 */
GoodsTypeHandler.editGoodsType = function() {
	var rowData = GoodsTypeHandler.goodsTypeGrid.getSelected();
	var rowsData = GoodsTypeHandler.goodsTypeGrid.getSelecteds();
	if(rowsData.length == 1){
		miniWin.afreshNewWin();
		miniWin.setParams(341, 170, "编辑商品类型[<font color='blue'>"	+ rowData.bgtName + "</font>]");
		miniWin.setOption("bodyStyle","padding:0px;");
	
		miniWin.load("goodsTypeForm.jsp",function(){
			var iframe = miniWin.getIFrameEl();
            var data = {action: "edit", bgtId: rowData.bgtId};
            iframe.contentWindow.GoodsTypeFormHandler.passData(data);
		},function(action){
			GoodsTypeHandler.goodsTypeGrid.reload();
		});
		
		miniWin.show();
	}else if(rowsData.length > 1){
		MiniuiTools.miniWarning("系统提示","不能同时编辑多条资源！");
	}else{
		MiniuiTools.miniWarning("系统提示","请选择要编辑的资源！");
	}
	
};

/**
 * 删除商品类型资源
 */
GoodsTypeHandler.delGoodsType = function() {
	var rowsData = GoodsTypeHandler.goodsTypeGrid.getSelecteds();
	if(rowsData.length > 0){
		mini.confirm("确认删除选择的" + rowsData.length + "条记录？" ,"系统提示",
			function(action, value){
				if (action == "ok") {
					var ids = "";
					for ( var i = 0; i < rowsData.length - 1; i++){
						ids += rowsData[i].bgtId+ ",";
					}
					ids += rowsData[rowsData.length - 1].bgtId;
					
					// 判断选择的商品类型是否拥有属性列表
					if(GoodsTypeHandler.getBgiByBgt(ids)){
						MiniuiTools.miniWarning("系统提示", "选择的记录存在类型属性关联，请检查后重试！");
						return;	
					}
					
					GoodsTypeHandler.goodsTypeGrid.loading();
                    $.ajax({
                        url: __root + "/delGoodsTypeByIds.do",
                        data: {
				        	bgtIds: ids,
				        	lslUser: gbOperateUser,
				    		lslAddress: gbOperateIp
				        },
                        success: function (text) {
                            MiniuiTools.miniInfo("系统提示","操作成功！", function(action){
				        		GoodsTypeHandler.goodsTypeGrid.reload();
				        	});
                        },
                        error: function (jqXHR, textStatus, errorThrown) {
        					MiniuiTools.miniError("系统提示","操作失败！" + jqXHR.responseText);
                        },
                        complete: function(e){
                        	GoodsTypeHandler.goodsTypeGrid.unmask();
                        }
                    });	
                } 
			}
		)
	}else{
		 MiniuiTools.miniWarning("系统提示","请选择要删除的资源！");
	}
};

/**
 * 检验选择的商品类型是否拥有属性列表
 * @param {} bsiIds
 * @return {}
 */
GoodsTypeHandler.getBgiByBgt = function(bgtIds){
	var isUsed = false;
	
	$.ajax({
        url: __root + "/getBgiByBgt.do?bgtIds=" + bgtIds,
        dataType: 'json',
        async: false,   // 同步执行，默认为true
        success: function (text) {
        	if(text == "1"){
        		isUsed = true;
        	}
        },
        error: function (jqXHR, textStatus, errorThrown) {
        	MiniuiTools.miniError("系统提示", "操作失败！" + jqXHR.responseText);
        }
    });
    
    return isUsed;
}

/**
 * 更新商品类型状态
 */
GoodsTypeHandler.updateGoodsTypeState = function() {
	if (!MiniuiTools.hasPurview("goods_type_edit")) {
		return;
	}
	var rowData = GoodsTypeHandler.goodsTypeGrid.getSelected();
	var bgtId = rowData.bgtId;	
	GoodsTypeHandler.goodsTypeGrid.loading();
	
    $.ajax({
        url: __root + "/updateGoodsTypeStateById.do",
        data: {
        	bgtId: bgtId,
        	lslUser: gbOperateUser,
    		lslAddress: gbOperateIp
        },
        success: function (text) {
            GoodsTypeHandler.goodsTypeGrid.reload();
        },
        error: function (jqXHR, textStatus, errorThrown) {
        	MiniuiTools.miniError("系统提示","操作失败！" + jqXHR.responseText);
        },
        complete: function(){
        	GoodsTypeHandler.goodsTypeGrid.unmask();
        }
    });	
};

/**
 * 根据商品类型编号查询商品类型属性列表
 */
GoodsTypeHandler.findBgtAttrById = function() {
	if (!MiniuiTools.hasPurview("goods_type_edit")) {
		mini.showMessageBox({title:"提示",message:"糟糕，您没有权限访问！",iconCls:"mini-messagebox-info",buttons: ["ok"]});
		return;
	}
	var rowData = GoodsTypeHandler.goodsTypeGrid.getSelected();
	var bgtId = rowData.bgtId;
	window.location.href="goodsTypeAttribute.jsp?bgtId="+bgtId;
};

/**
 * 展开或折叠查询商品类型面板
 */
GoodsTypeHandler.queryGoodsType = function() {
	if (GoodsTypeHandler.isQueryBtnExpended) {
		GoodsTypeHandler.isQueryBtnExpended = false;
		mini.get("goods_type_query").setText("展开查询");
		mini.get("goods_type_query").setIconCls("icon-btn-expand");
	} else {
		GoodsTypeHandler.isQueryBtnExpended = true;
		mini.get("goods_type_query").setText("折叠查询");
		mini.get("goods_type_query").setIconCls("icon-btn-collapse");
	}
	
	$("#queryGoodsTypeDiv").toggle(500);
    setTimeout(function(){mini.get("goodsTypePanel").doLayout();},600);
};

/**
 * 执行查询商品类型的方法
 */
GoodsTypeHandler.doQueryGoodsType = function(){
	var bgtName = mini.get("bgtName").getValue();
	var bgtState = mini.get("bgtState").getValue();
	
    GoodsTypeHandler.goodsTypeGrid.load({typeName: bgtName, typeState: bgtState});
};

/**
 * 刷新页面数据及布局
 */
GoodsTypeHandler.afresh = function(){
	GoodsTypeHandler.doQueryGoodsType();
	mini.get("goodsTypePanel").doLayout();
}

/**
 * DrawCell 绘制单元格
 */
GoodsTypeHandler.goodsTypeGrid.on("drawcell", function(e) {
	 var record = e.record;
     var column = e.column;
     var field = e.field;
     var value = e.value;

     if (column.field == "bgtState") {
        if (e.value == 1) {
            e.cellHtml = "<span class='icon-show-yes' style='cursor: pointer;' onclick='GoodsTypeHandler.updateGoodsTypeState()'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>";                    
        } else {
            e.cellHtml = "<span class='icon-show-no' style='cursor: pointer;' onclick='GoodsTypeHandler.updateGoodsTypeState()'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>";
        }
    }
    
    if(column.field == "bgtOperation"){
    	e.cellHtml = "<span><a style='cursor: pointer;' onclick='GoodsTypeHandler.findBgtAttrById()'>属性列表</a></span>";
    }
});

